{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-07-05T02:24:43.897666Z",
     "start_time": "2024-07-05T02:24:43.891609800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "知识点：变量\n",
    "学习目标：\n",
    "    1. 理解什么是变量以及掌握变量的定义方式\n",
    "\n",
    "变量的定义格式： 变量名 = 变量值\n",
    "\"\"\"\n",
    "\n",
    "# 定义一个变量：钱包, 用来存储余额数字\n",
    "\n",
    "# 查看一下，钱包还剩余多少钱\n",
    "\n",
    "#type获取字面量、变量、表达式等等的类型\n",
    "\n",
    "a = \"hello\"\n",
    "tp = type(a)\n",
    "print(tp) # <class 'str'>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'str'>\n",
      "<class 'int'>\n",
      "<class 'int'>\n",
      "hello123\n",
      "hello456\n",
      "hellow\n",
      "    world\n",
      "    !!!\n"
     ]
    }
   ],
   "source": [
    "#变量a可以赋值为不同类型\n",
    "a = 10\n",
    "tp = type(a)\n",
    "print(tp) # <class 'int'>\n",
    "\n",
    "print(type(\"aaa\")) # <class 'str'>\n",
    "\n",
    "print(type(12)) # <class 'int'>\n",
    "\n",
    "print(type(1 + 123)) # <class 'int'>\n",
    "\n",
    "# 字符串表示方法：单引号、双引号，三个双引号\n",
    "print('hello123')\n",
    "print(\"hello456\")\n",
    "print(\"\"\"hellow\n",
    "    world\n",
    "    !!!\"\"\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T02:25:08.069754200Z",
     "start_time": "2024-07-05T02:25:08.061625600Z"
    }
   },
   "id": "34eb3213d31486ca"
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "124\n",
      "234\n",
      "123.456\n",
      "123\n"
     ]
    }
   ],
   "source": [
    "#变量类型转换\n",
    "a2 = \"123\"\n",
    "b2 = int(a2)\n",
    "print(b2 + 1) # 124\n",
    "print(str(234))\n",
    "print(float(\"123.456\"))\n",
    "print(int(123.8)) # 123"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T02:30:48.210270300Z",
     "start_time": "2024-07-05T02:30:48.199797100Z"
    }
   },
   "id": "9ce47d21fb80cf2"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.5\n",
      "4\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# 算术运算\n",
    "print(9/2) # 4.5  除法取全部\n",
    "print(9//2) # 4   //除法取整\n",
    "print(9%2) # 1 除法取余\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T02:31:28.394376Z",
     "start_time": "2024-07-05T02:31:28.380873Z"
    }
   },
   "id": "b6e4208ccbe834d2"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi,Jack\n",
      "Hi,Rose,I am Jack\n"
     ]
    }
   ],
   "source": [
    "#字符串拼接\n",
    "print(\"Hi,\" + \"Jack\")\n",
    "a3 = \"Rose\"\n",
    "print(\"Hi,\" + a3 + \",I am Jack\")\n",
    "# print(\"Hi,\" + 123) # 加号前后变量类型必须是str，否则会报错"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T02:31:33.648548300Z",
     "start_time": "2024-07-05T02:31:33.637581500Z"
    }
   },
   "id": "20fbe37a99f7850a"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi, my name is Tom, and I am 10 years old. 108.20\n",
      "hello,Tom,108.2\n"
     ]
    }
   ],
   "source": [
    "#字符串格式化，%s 字符串，%d 整数，%f 浮点数\n",
    "name = \"Tom\"\n",
    "age = 10\n",
    "f = 108.2\n",
    "a4 = \"Hi, my name is %s, and I am %d years old. %5.2f\" % (name,age,f) # Hi, my name is Tom, and I am 10 years old. 108.20\n",
    "print(a4)\n",
    "# 优雅的格式化写法，前面加一个f，里面用大括号将变量括起来，但是不能控制浮点数的精度\n",
    "print(f\"hello,{name},{f}\") # hello,Tom,108.2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T02:31:45.061927800Z",
     "start_time": "2024-07-05T02:31:45.050340300Z"
    }
   },
   "id": "af35a935b45513cd"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# input函数读取标准输入，如论标准输入写的啥，input()函数返回类型都是str\n",
    "a4 = input()\n",
    "print(a4)\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5d1be11b3a1ceed3"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "变量是整数类型\n",
      "变量是整数类型\n"
     ]
    }
   ],
   "source": [
    "# python如何判断变量类型是否为int\n",
    "# 在Python中，你可以使用内置的isinstance()函数来判断一个变量是否是整数（int）类型。这里是一个例子：\n",
    "variable = 10\n",
    " \n",
    "if isinstance(variable, int):\n",
    "    print(\"变量是整数类型\")\n",
    "else:\n",
    "    print(\"变量不是整数类型\")\n",
    "    \n",
    "# 如果你想要更加严谨地检查，也可以使用type()函数，但是通常情况下isinstance()就足够了，因为它能够处理子类的情况。\n",
    "variable = 10\n",
    " \n",
    "if type(variable) is int:\n",
    "    print(\"变量是整数类型\")\n",
    "else:\n",
    "    print(\"变量不是整数类型\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T02:33:16.759496100Z",
     "start_time": "2024-07-05T02:33:16.752714300Z"
    }
   },
   "id": "b7689c924b4192e6"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
